package com.itheima;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
/*
    获取请求头信息的相关方法
 */

@WebServlet("/servletDemo02")
public class ServletDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.根据请求头名称获取一个值 getHeader()
        String connection = req.getHeader("connection");
        System.out.println(connection);

        System.out.println("-------");
        //2.根据请求头名称获取多个值 getHeaders
        Enumeration<String> headers = req.getHeaders("accept-encoding");
        while (headers.hasMoreElements()) {
            String value = headers.nextElement();
            System.out.println(value);

        }


        System.out.println("-------");
        //3.获取所有的请求头名称  getHeaderNames()
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            String value = req.getHeader(name);
            System.out.println(name+","+value);
        }

        System.out.println("-------");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
